<script lang="ts">
import {defineComponent, ref, PropType} from "vue"
import request from "@/units/request"

export default defineComponent({
  name: "AppSelect",
  props: {
    api: {
      type: String
    },
    option: {
      type: Array as PropType<Array<{ value: string | number, label: string }>>
    }
  },
  setup(props) {
    const data = ref<Array<{ value: string | number, label: string }>>([])

    if (props.api) {
      //获取字典数据
      request(props.api, {}, 'GET', false).then((result: any) => {
        if (result.code === 200) {
          data.value = result.data
        }
      })
    } else if (props.option instanceof Array) {
      //复制内容
      data.value = [...props.option]
    }
    return {data}
  }
})
</script>

<template>
  <el-select clearable v-bind="$attrs">
    <el-option v-for="(item,index) in data" :key="index" :label="item.label" :value="item.value"/>
  </el-select>
</template>